{% extends 'base.html' %}

{% block title %}API管理{% endblock %}

{% block content %}
<div class="container-fluid">
    <div class="row mb-3">
        <div class="col">
            <h2>API管理</h2>
        </div>
    </div>
    
    <div class="row mb-3">
        <div class="col-md-6">
            <div class="input-group">
                <input type="text" id="search-input" class="form-control" placeholder="搜索API...">
                <button class="btn btn-outline-secondary" type="button" id="search-btn">
                    <i class="bi bi-search"></i> 搜索
                </button>
            </div>
        </div>
        <div class="col-md-6 text-end">
            <button class="btn btn-success me-2" data-bs-toggle="modal" data-bs-target="#importModal">
                <i class="bi bi-cloud-upload"></i> 导入API
            </button>
            <button class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#addEditModal">
                <i class="bi bi-plus-circle"></i> 新增API
            </button>
        </div>
    </div>
    
    <div class="row">
        <div class="col">
            <div class="card">
                <div class="card-body">
                    <div class="table-responsive">
                        <table class="table table-hover">
                            <thead>
                                <tr>
                                    <th>名称</th>
                                    <th>方法</th>
                                    <th>路径</th>
                                    <th>标签</th>
                                    <th>更新时间</th>
                                    <th>操作</th>
                                </tr>
                            </thead>
                            <tbody id="api-list">
                                <!-- API列表将通过JavaScript动态加载 -->
                            </tbody>
                        </table>
                    </div>
                    
                    <div id="pagination" class="d-flex justify-content-between align-items-center mt-3">
                        <div>
                            <span id="pagination-info">显示 1-10 条，共 0 条</span>
                        </div>
                        <nav>
                            <ul class="pagination">
                                <!-- 分页将通过JavaScript动态加载 -->
                            </ul>
                        </nav>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<!-- 导入API模态框 -->
{% include 'api/import_modal.html' %}

<!-- 新增/编辑API模态框 -->
{% include 'api/add_edit_modal.html' %}

<!-- 查看API模态框 -->
{% include 'api/view_modal.html' %}

<!-- 删除API确认模态框 -->
{% include 'api/delete_modal.html' %}

{% endblock %}

{% block api_scripts %}
<script src="{{ url_for('static', filename='js/api_functions_part1.js') }}"></script>
<script src="{{ url_for('static', filename='js/api_functions_part2.js') }}"></script>
{% endblock %}

{% block scripts %}
{{ super() }}
<script>
    // 等待依赖库和API函数加载完成
    function waitForDependencies(callback) {
        if (typeof jQuery !== 'undefined' && typeof bootstrap !== 'undefined' && typeof loadApiList !== 'undefined') {
            callback();
        } else {
            setTimeout(function() {
                waitForDependencies(callback);
            }, 100);
        }
    }
    
    waitForDependencies(function() {
        $(document).ready(function() {
            // API列表已在api_functions_part1.js中加载，这里不再重复加载
            
            // 搜索按钮点击事件
            $('#search-btn').click(function() {
                loadApiList(1);
            });
            
            // 回车键搜索
            $('#search-input').keypress(function(e) {
                if (e.which === 13) {
                    loadApiList(1);
                }
            });
            
            // 分页点击事件
            $(document).on('click', '#pagination .page-link', function(e) {
                e.preventDefault();
                const page = $(this).data('page');
                if (page) {
                    loadApiList(page);
                }
            });
            
            // 编辑API
            $(document).on('click', '.edit-api', function() {
                const apiId = $(this).data('id');
                editApi(apiId);
            });
            
            // 删除API
            $(document).on('click', '.delete-api', function() {
                const apiId = $(this).data('id');
                const apiName = $(this).closest('tr').find('td:first').text();
                confirmDeleteApi(apiId, apiName);
            });
            
            // 导入API表单提交
            $('#import-form').submit(function(e) {
                e.preventDefault();
                importApi();
            });
            
            // 新增/编辑API表单提交
            $('#add-edit-form').submit(function(e) {
                e.preventDefault();
                saveApi();
            });
            
            // 确认删除API - 使用confirmDeleteApi函数中设置的数据
            $('#confirm-delete-btn').click(function() {
                const apiId = $(this).attr('data-id');
                console.log('确认删除按钮点击，API ID:', apiId);
                if (apiId) {
                    deleteApi(apiId);
                } else {
                    console.error('未找到API ID');
                    showToast('error', '删除操作失败：未找到API ID');
                }
            });
        });
    });
</script>
{% endblock %}